﻿Imports VendasDAL
Imports Conection
Public Class MarcaBLL
    Dim ObjDAL As New MarcaDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Descricao As String
    Property Status As Integer
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer)
        Me.Codigo = codigo
        Me.Descricao = descricao
        Me.Status = status
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Boolean
        Try
            If (Trim(Descricao) <> "") Then
                If (objfuncoes.ValidaReplica(Me.Descricao, 0, "Vendas.Gen_Marca", "mar_codigo", "mar_descricao").Rows.Count = 0) Then
                    ObjDAL = New MarcaDAL
                    Me.Codigo = ObjDAL.Inserir(Me.Descricao, Me.Status)
                Else : Throw New Exception("Marca já cadastrada")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            ObjDAL = New MarcaDAL
            If (Descricao <> "") Then
                If (objfuncoes.ValidaReplica(Me.Descricao, Me.Codigo, "Vendas.Gen_Marca", "mar_codigo", "mar_descricao").Rows.Count = 0) Then
                    If (ObjDAL.ValidaExcluir(Me.Codigo, Me.Status)) Then
                        ret = ObjDAL.Alterar(Me.Descricao, Me.Status, Me.Codigo)
                    Else : Throw New Exception("Esta marca não pode ser modificada, verifique antes a ocorrência da mesma no(s) formulário(s): Carro")
                    End If
                Else : Throw New Exception("Marca já cadastrado")
                End If
            End If


        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ObjDAL = New MarcaDAL
            If (ObjDAL.ValidaExcluir(Me.Codigo, Me.Status)) Then
                ret = ObjDAL.Deletar(Codigo)
            Else
                Throw New Exception("Esta marca não pode ser modificada, verifique antes a ocorrência da mesma no(s) formulário(s): Carro")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New MarcaDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region

#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New MarcaDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("mar_codigo").ToString)
                Me.Descricao = tab.Rows(0)("mar_descricao").ToString
                Me.Status = tab.Rows(0)("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Descricao = Nothing
        Me.Status = Nothing
    End Sub
#End Region
End Class
